import { Dispatcher } from "../../../../mvc/core/Dispatcher";
import { DataEvent } from "../../../events/DataEvent";
import { EventName } from "../../../mvc/EventName";
import { ObjCreate } from "../../common/ObjCreate";
import { Language } from "../../../../../extend/language/Language";
import { GlobalStyle } from "../../../../common/global/GlobalStyle";
import { ResFileConst } from "../../../resource/ResFileConst";
import { FinalTree } from "./tree/FinalTree";
import { LoaderHelp } from "../../../../common/display/LoaderHelp";
import { GlobalClass } from "../../../../../com/mui/core/GlobalClass";
import { ImagesConst } from "../../../resource/ImagesConst";
import { EArenaFinalPosType } from "../../../../../Message/Public/EArenaFinalPosType";
import { SArenaCrossChooseFinalPos } from "../../../../../Message/Public/SArenaCrossChooseFinalPos";
import { GButton } from "../../../../../com/mui/controls/GButton";
import { FrameTimer } from "../../../../../com/gengine/core/frame/FrameTimer";
import { TimerType } from "../../../../../com/gengine/core/frame/TimerType";
import { Alert } from "../../../../../com/mui/controls/Alert";
import { GLoadedButton } from "../../../../../com/mui/controls/GLoadedButton";
type int = number;
//class PositionSelectPanel
    
    export  class PositionSelectPanel /* flash.display.Sprite */
    {
        constructor()
        {
            
            this._aryBtns = [];
            this._aryFlashPos = [];
            this.init();
            this.addEventListener(flash.events.Event.ADDED_TO_STAGE, this.onAddToStage);
            return;
        }

        /* internal  */onAddToStage(arg1: Object /* flash.events.Event */): void
        {
            Dispatcher.dispatchEvent(new DataEvent(EventName.ArenaCrossGetFinalPos));
            return;
        }

        /* internal  */init(): void
        {
            var loc4=0;
            var loc5=null;
            var loc6=null;
            var loc1=0;
            while (loc1 < 32) 
            {
                loc4 = loc1 >= 16 ? 674 : 8;
                loc5 = ObjCreate.createGButton(Language.getString(44355), loc4, 18 + loc1 % 16 * 30, 120, 20, this, "ButtonBlueStyle");
                this._aryBtns.push(loc5);
                (loc6 = new Object /* flash.accessibility.AccessibilityProperties */()).description = (loc1 + 1).toString();
                loc5.accessibilityProperties = loc6;
                ++loc1;
            }
            var loc2=GlobalStyle.textFormat6;
            loc2.size = 13;
            ObjCreate.createTextField(Language.getString(44356), 363, 40, 105, 20, this, loc2);
            loc2 = GlobalStyle.textFormatGreen;
            loc2.bold = true;
            loc2.size = 14;
            ObjCreate.createTextField(Language.getString(44357), 315, 63, 240, 20, this, loc2);
            this._bmpBg = new Object /* flash.display.Bitmap */();
            this._bmpBg.x = 215;
            this._bmpBg.y = 89;
            this.addChild(this._bmpBg);
            this._bmpDoubt = new Object /* flash.display.Bitmap */();
            this._bmpDoubt.x = 386;
            this._bmpDoubt.y = 226;
            this.addChild(this._bmpDoubt);
            this._bmpKemp = new Object /* flash.display.Bitmap */();
            this._bmpKemp.x = 335;
            this._bmpKemp.y = 177;
            this.addChild(this._bmpKemp);
            this._btnRandom = ObjCreate.createGLoadedButton(ResFileConst.ArenaCrossRandomPos, 354, 93, 97, 50, this);
            this._btnRandom.addEventListener(flash.events.MouseEvent.CLICK, this.onRandomPos);
            this._finalTree = new FinalTree();
            this._finalTree.x = 128;
            this._finalTree.y = 28;
            this.addChild(this._finalTree);
            loc2 = GlobalStyle.textFormat1;
            loc2.leading = 7;
            var loc3=ObjCreate.createTextField("", 313, 317, 180, 120, this, loc2);
            loc3.multiline = true;
            loc3.wordWrap = true;
            loc3.htmlText = Language.getString(44358);
            this._btnArenaRule = ObjCreate.createGButton(Language.getString(44359), 362, 457, 68, 25, this, "ArenaRedBtn");
            LoaderHelp.addResCallBack(ResFileConst.ArenaCrossFinalSelectPos, this.onResCompl);
            this._btnArenaRule.addEventListener(flash.events.MouseEvent.CLICK, this.onClickBtnRule);
            return;
        }

        /* internal  */onRandomPos(arg1: Object /* flash.events.MouseEvent */): void
        {
            if (this._isInFlashing) 
            {
                return;
            }
            this._isClickMoment = true;
            Dispatcher.dispatchEvent(new DataEvent(EventName.ArenaCrossFinalSelectPos, 1));
            return;
        }

        /* internal  */onResCompl(): void
        {
            this._bmpBg.bitmapData = GlobalClass.getBitmapData(ImagesConst.finalSelectPosbg);
            this._bmpDoubt.bitmapData = GlobalClass.getBitmapData(ImagesConst.doubt);
            this._bmpKemp.bitmapData = GlobalClass.getBitmapData(ImagesConst.airKemp);
            this._isResCompl = true;
            this.updateKempBitmapData();
            return;
        }

        /* internal  */onClickBtnRule(arg1: Object /* flash.events.MouseEvent */): void
        {
            Dispatcher.dispatchEvent(new DataEvent(EventName.KingWarRuleDescToTabBar, 2));
            return;
        }

        /* internal  */updateKempBitmapData(): void
        {
            if (this._isResCompl && this._arenaCrossChooseFinalpos) 
            {
                if (this._arenaCrossChooseFinalpos.finalPosType != EArenaFinalPosType._EArenaFinalPosDown) 
                {
                    this._bmpKemp.bitmapData = GlobalClass.getBitmapData(ImagesConst.airKemp);
                }
                else 
                {
                    this._bmpKemp.bitmapData = GlobalClass.getBitmapData(ImagesConst.earthKemp);
                }
            }
            return;
        }

        public updateFinalPos(arg1: SArenaCrossChooseFinalPos): void
        {
            var loc3=null;
            var loc4=false;
            var loc5=0;
            this._arenaCrossChooseFinalpos = arg1;
            this._myPos = arg1.myFinalPos;
            var loc1=this._myPos > 0;
            this.updateKempBitmapData();
            var loc2=0;
            while (loc2 < this._aryBtns.length) 
            {
                loc3 = this._aryBtns[loc2] as GButton;
                if (loc2 + 1 != arg1.myFinalPos) 
                {
                    loc3.styleName = "ButtonBlueStyle";
                    loc4 = false;
                    loc5 = 0;
                    while (loc5 < arg1.positions.length) 
                    {
                        if (arg1.positions[loc5] == loc2 + 1) 
                        {
                            loc4 = true;
                            loc3.label = Language.getString(44361);
                            loc3.enabled = false;
                            break;
                        }
                        ++loc5;
                    }
                    if (!loc4) 
                    {
                        loc3.label = Language.getString(44355);
                    }
                }
                else 
                {
                    if (this._isClickMoment) 
                    {
                        this.flashing(loc2);
                        this._isClickMoment = false;
                        loc3.label = Language.getString(44355);
                        loc3.styleName = "ButtonBlueStyle";
                    }
                    else if (!this._isInFlashing) 
                    {
                        loc3.styleName = "ButtonGreenStyle";
                        loc3.label = Language.getString(44360);
                    }
                    this._btnRandom.enabled = false;
                }
                loc3.drawNow();
                ++loc2;
            }
            return;
        }

        /* internal  */flashing(arg1: int): void
        {
            if (this._isInFlashing) 
            {
                return;
            }
            this._finalPos = arg1;
            this._currentArrayPos = 0;
            this._lastArrayPos = -1;
            this._circleNum = 0;
            this._isInFlashing = true;
            this.setFlashBtns();
            if (!this._timer) 
            {
                this._timer = new FrameTimer();
                this._timer.addListener(TimerType.ENTERFRAME, this.onTimerEnterFrame);
            }
            this._timer.start();
            return;
        }

        /* internal  */onTimerEnterFrame(arg1: FrameTimer): void
        {
            if (this._circleNum == 0 && this._timer.currentCount % 2 == 0 || this._circleNum == 1 && this._timer.currentCount % 3 == 0 || this._circleNum >= 2 && this._timer.currentCount % 6 == 0) 
            {
                this.nextSelectPos();
            }
            return;
        }

        /* internal  */nextSelectPos(): void
        {
            var loc3=0;
            var loc4=null;
            if (this._lastArrayPos >= 0) 
            {
                loc3 = this._aryFlashPos[this._lastArrayPos];
                (loc4 = this._aryBtns[loc3]).styleName = "ButtonBlueStyle";
            }
            var loc1=this._aryFlashPos[this._currentArrayPos];
            var loc2=this._aryBtns[loc1];
            loc2.styleName = "ButtonGreenStyle";
            if (this._circleNum >= 2 && this._finalPos == loc1) 
            {
                this._timer.stop();
                this._isInFlashing = false;
                this.updateFinalPos(this._arenaCrossChooseFinalpos);
                return;
            }
            this._lastArrayPos = this._currentArrayPos;
            var loc5;
            var loc6=((loc5 = this)._currentArrayPos + 1);
            loc5._currentArrayPos = loc6;
            if (this._currentArrayPos >= this._aryBtns.length) 
            {
                this._currentArrayPos = 0;
                loc6 = ((loc5 = this)._circleNum + 1);
                loc5._circleNum = loc6;
            }
            return;
        }

        /* internal  */setFlashBtns(): void
        {
            var loc2=0;
            this._aryFlashPos = [];
            var loc1=0;
            while (loc1 < this._aryBtns.length) 
            {
                if (loc1 + 1 != this._arenaCrossChooseFinalpos.myFinalPos) 
                {
                    loc2 = 0;
                    while (loc2 < this._arenaCrossChooseFinalpos.positions.length) 
                    {
                        if (this._arenaCrossChooseFinalpos.positions[loc2] == loc1 + 1) 
                        {
                            break;
                        }
                        ++loc2;
                    }
                    this._aryFlashPos.push(loc1);
                }
                else 
                {
                    this._aryFlashPos.push(loc1);
                }
                ++loc1;
            }
            return;
        }

        /* internal  */onClickSelectBtn(arg1: Object /* flash.events.MouseEvent */): void
        {
            var e: Object /* flash.events.MouseEvent */;
            var btn: GButton;
            var btnPos: int;
            var onSelect: Function;

            var loc1;
            btn = null;
            btnPos = 0;
            e = arg1;
            if (this._myPos <= 0) 
            {
                onSelect = (arg1: int): void
                {
                    if (arg1 == Alert.OK) 
                    {
                        Dispatcher.dispatchEvent(new DataEvent(EventName.ArenaCrossFinalSelectPos, btnPos));
                    }
                    return;
                }
                btn = e.target as GButton;
                btnPos = int(btn.accessibilityProperties.description);
                Alert.show(Language.getString(44362), null, Alert.OK | Alert.CANCEL, null, onSelect);
            }
            return;
        }

        /* internal */ /* var */_aryBtns: Array<any>;

        /* internal */ /* var */_finalTree: FinalTree;

        /* internal */ /* var */_bmpBg: Object /* flash.display.Bitmap */;

        /* internal */ /* var */_bmpDoubt: Object /* flash.display.Bitmap */;

        /* internal */ /* var */_bmpKemp: Object /* flash.display.Bitmap */;

        /* internal */ /* var */_btnRandom: GLoadedButton;

        /* internal */ /* var */_btnArenaRule: GButton;

        /* internal */ /* var */_myPos: int=0;

        /* internal */ /* var */_arenaCrossChooseFinalpos: SArenaCrossChooseFinalPos;

        /* internal */ /* var */_isResCompl: Boolean=false;

        /* internal */ /* var */_isClickMoment: Boolean=false;

        /* internal */ /* var */_isInFlashing: Boolean=false;

        /* internal */ /* var */_finalPos: int=0;

        /* internal */ /* var */_lastArrayPos: int=0;

        /* internal */ /* var */_currentArrayPos: int=0;

        /* internal */ /* var */_circleNum: int=0;

        /* internal */ /* var */_aryFlashPos: Array<any>;

        /* internal */ /* var */_timer: FrameTimer;
    }
